#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/27 23:36
# @Author : lixinpan
# @Site : 
# @File : word_calculate.py
# @function:
import sys
import re

WORD_RE = re.compile(r'\w+')
index = {}

with open('test.txt', encoding='utf-8') as fp:
    for line_no, line in enumerate(fp, 1):
        for match in WORD_RE.finditer(line):
            print("match是：{}".format(match))
            # 返回match匹配到的内容
            word = match.group()
            # match.start(),返回match内容的起始位置
            column_no = match.start() + 1

            location = (line_no, column_no)

            # 如果存在该word，返回对应list；否则返回空list
            occurrences = index.get(word, [])
            occurrences.append(location)
            # 将数据信息纳入到index索引里面
            index[word] = occurrences

for word in sorted(index, key=str.upper):
    print(word, index[word])
